{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "On branch modis_scripts\n",
      "Your branch is up to date with 'origin/modis_scripts'.\n",
      "\n",
      "Changes not staged for commit:\n",
      "\t\u001b[31mmodified:   get_modis_gdal_post06.sh\u001b[m\n",
      "\n",
      "Untracked files:\n",
      "\t\u001b[31mUntitled.ipynb\u001b[m\n",
      "\t\u001b[31m../notebooks/untitled.txt\u001b[m\n",
      "\n",
      "no changes added to commit\n"
     ]
    }
   ],
   "source": [
    "#! git commit -m \"added docu\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Saved working directory and index state WIP on modis_scripts: fbd3302 added bash scripts to download and remap MODIS data from tile server\n"
     ]
    }
   ],
   "source": [
    "#!git stash"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#git branch delete modis_scripts\n",
    "#git push origin --delete modis_scripts\n",
    "#git config --global user.email schweig@uw.edu \n",
    "#git config --global user.name axelschweiger\n",
    "#git push origin --delete modis_scripts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cloning into 'gibs-ml'...\n",
      "remote: Enumerating objects: 326, done.\u001b[K\n",
      "remote: Counting objects: 100% (326/326), done.\u001b[K\n",
      "remote: Compressing objects: 100% (176/176), done.\u001b[K\n",
      "remote: Total 326 (delta 146), reused 326 (delta 146), pack-reused 0\u001b[K\n",
      "Receiving objects: 100% (326/326), 17.29 MiB | 31.73 MiB/s, done.\n",
      "Resolving deltas: 100% (146/146), done.\n",
      "Checking out files: 100% (55/55), done.\n"
     ]
    }
   ],
   "source": [
    "# Clone the repository with a glib layer definition\n",
    "#!git clone https://github.com/nasa-gibs/gibs-ml.git"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "from datetime import datetime, time, date\n",
    "import os\n",
    "import subprocess\n",
    "sys.path.append('/home/jovyan/overcast/gibs-ml')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['/home/jovyan/overcast/modis_scripts',\n",
       " '/srv/conda/envs/notebook/lib/python37.zip',\n",
       " '/srv/conda/envs/notebook/lib/python3.7',\n",
       " '/srv/conda/envs/notebook/lib/python3.7/lib-dynload',\n",
       " '',\n",
       " '/srv/conda/envs/notebook/lib/python3.7/site-packages',\n",
       " '/srv/conda/envs/notebook/lib/python3.7/site-packages/IPython/extensions',\n",
       " '/home/jovyan/.ipython',\n",
       " 'gib-ml',\n",
       " '../gib-ml',\n",
       " '../gib-ml',\n",
       " '/home/jovyan/overcast/gib-ml']"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sys.path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gibs_layer import GIBSLayer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [],
   "source": [
    "# this is from an example in the gibs-ml\n",
    "layer_name='VIIRS_SNPP_CorrectedReflectance_BandsM11_I2_I1'\n",
    "layer_name='MODIS_Terra_CorrectedReflectance_TrueColor'\n",
    "layer = GIBSLayer.get_gibs_layer(layer_name)\n",
    "epsg='3413'\n",
    "start_date = datetime.strptime(\"2019-06-10\", \"%Y-%m-%d\")\n",
    "tile_resolution='250m'\n",
    "datestring = start_date.strftime(\"%Y-%m-%d\")\n",
    "datestring\n",
    "output_dir='/home/jovyan/shared/data-overcast/modis/'\n",
    "output_dir='./'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'<GDAL_WMS><Service name=\"TMS\"><ServerUrl>https://gibs.earthdata.nasa.gov/wmts/epsg3413/best/MODIS_Terra_CorrectedReflectance_TrueColor/default/{Time}/250m/${z}/${y}/${x}.jpg</ServerUrl></Service><DataWindow><UpperLeftX>-4194304</UpperLeftX><UpperLeftY>4194304</UpperLeftY><LowerRightX>4194304</LowerRightX><LowerRightY>-4194304</LowerRightY><TileLevel>5</TileLevel><TileCountX>2</TileCountX><TileCountY>2</TileCountY><YOrigin>top</YOrigin></DataWindow><Projection>EPSG:3413</Projection><BlockSizeX>512</BlockSizeX><BlockSizeY>512</BlockSizeY><BandsCount>3</BandsCount><Timeout>3000</Timeout></GDAL_WMS>'"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "layer.generate_xml(protocol=\"tms\", epsg=epsg, tile_resolution=tile_resolution, datestring=datestring)\n",
    "xml=layer.gibs_xml\n",
    "# this seems to generate something that looks like and XML file. It seems to be a string\n",
    "xml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {},
   "outputs": [],
   "source": [
    "xml = xml.replace(\"{Time}\", datestring)\n",
    "# IMPORTANT ON WINDOWS: REPLACE \" with '\n",
    "xml = xml.rstrip()  # remove all trailing whitespace\n",
    "# infile = infile.replace(\"\\\"\", \"'\")\n",
    "\n",
    "# Build name of output file\n",
    "#xml = \"'\" + xml + \"'\"  # add extra quotes around XML\n",
    "with open(\"temp.xml\", \"w\") as f:\n",
    "    f.write(xml)\n",
    "outfile = os.path.join(output_dir, 'modis_output_test.tif')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [],
   "source": [
    "proj_str =\"'+proj=stere +lat_0=90 +lat_ts=70 +lon_0=-145 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs'\" \n",
    "te_str = '-800000 -2500000 1200000 -400000'\n",
    "tr_str = '250 250'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "# gdalwarp -overwrite -t_srs '+proj=stere +lat_0=90 +lat_ts=70 +lon_0=-145 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs' -te -800000 -2500000 1200000 -400000 -tr 250 250 $xmlfile $outfile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "gdalwarp -overwrite -t_srs '+proj=stere +lat_0=90 +lat_ts=70 +lon_0=-145 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs' -te -800000 -2500000 1200000 -400000 -tr 250 250 temp.xml ./modis_output_test.tif\n"
     ]
    }
   ],
   "source": [
    "cmd_list = [\"gdalwarp\",\"-overwrite\",\"-t_srs\",proj_str,\"-te\",te_str,\"-tr\",tr_str,\"temp.xml\", outfile]\n",
    "cmd= ' '.join(cmd_list)\n",
    "#cmd=\"'\"+cmd+\"'\"\n",
    "print(cmd)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'ls -l'\n"
     ]
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "gdalwarp -overwrite -t_srs '+proj=stere +lat_0=90 +lat_ts=70 +lon_0=-145 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs' -te -800000 -2500000 1200000 -400000 -tr 250 250 temp.xml ./modis_output_test.tif\n",
      "b'Creating output file that is 8000P x 8400L.\\nProcessing temp.xml [1/1] : 0...10...20...30...40...50...60...70...80...90...100 - done.\\n'\n",
      "here\n"
     ]
    }
   ],
   "source": [
    "#process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,shell='True')\n",
    "print(cmd)\n",
    "process = subprocess.Popen(cmd,shell='True',stdout=subprocess.PIPE,stderr=subprocess.PIPE)\n",
    "out,err = process.communicate()\n",
    "print(process.returncode, out,err, )\n",
    "print(\"Done Writing File :\" outfile)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(b'Creating output file that is 8000P x 8400L.\\nProcessing temp.xml [1/1] : 0...10...20...30...40...50...60...70...80...90...100 - done.\\n',\n",
       " b'')"
      ]
     },
     "execution_count": 141,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "out, err"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: '/srv/conda/envs/notebook/bin/gdalwarp -h': '/srv/conda/envs/notebook/bin/gdalwarp -h'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-94-661f6dfdeb72>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprocess\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msubprocess\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mPopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcmd\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstdout\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0msubprocess\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mPIPE\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstderr\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0msubprocess\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mPIPE\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m/srv/conda/envs/notebook/lib/python3.7/subprocess.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, encoding, errors, text)\u001b[0m\n\u001b[1;32m    798\u001b[0m                                 \u001b[0mc2pread\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mc2pwrite\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    799\u001b[0m                                 \u001b[0merrread\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0merrwrite\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 800\u001b[0;31m                                 restore_signals, start_new_session)\n\u001b[0m\u001b[1;32m    801\u001b[0m         \u001b[0;32mexcept\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    802\u001b[0m             \u001b[0;31m# Cleanup if the child failed starting.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/srv/conda/envs/notebook/lib/python3.7/subprocess.py\u001b[0m in \u001b[0;36m_execute_child\u001b[0;34m(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, restore_signals, start_new_session)\u001b[0m\n\u001b[1;32m   1549\u001b[0m                         \u001b[0;32mif\u001b[0m \u001b[0merrno_num\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0merrno\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mENOENT\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1550\u001b[0m                             \u001b[0merr_msg\u001b[0m \u001b[0;34m+=\u001b[0m \u001b[0;34m': '\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mrepr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0merr_filename\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1551\u001b[0;31m                     \u001b[0;32mraise\u001b[0m \u001b[0mchild_exception_type\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0merrno_num\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0merr_msg\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0merr_filename\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1552\u001b[0m                 \u001b[0;32mraise\u001b[0m \u001b[0mchild_exception_type\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0merr_msg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1553\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: '/srv/conda/envs/notebook/bin/gdalwarp -h': '/srv/conda/envs/notebook/bin/gdalwarp -h'"
     ]
    }
   ],
   "source": [
    "process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Sample XML file used by my bash script.\n",
    "# <GDAL_WMS>\n",
    "#     <Service name=\"TMS\">\n",
    "#         <ServerUrl>http://map1.vis.earthdata.nasa.gov/wmts-arctic/MODIS_Terra_CorrectedReflectance_REPLACE_THIS_STRING_WITH_TYPE/default/REPLACE_THIS_STRING_WITH_DATE/EPSG3413_250m/${z}/${y}/${x}.jpg</ServerUrl>\n",
    "#     </Service>\n",
    "#     <DataWindow>\n",
    "#         <UpperLeftX>-4194304</UpperLeftX>\n",
    "#         <UpperLeftY>4194304</UpperLeftY>\n",
    "#         <LowerRightX>4194304</LowerRightX>\n",
    "#         <LowerRightY>-4194304</LowerRightY>\n",
    "#         <TileLevel>5</TileLevel>\n",
    "#         <TileCountX>2</TileCountX>\n",
    "#         <TileCountY>2</TileCountY>\n",
    "#         <YOrigin>top</YOrigin>\n",
    "#     </DataWindow>\n",
    "#     <Projection>EPSG:3413</Projection>\n",
    "#     <BlockSizeX>512</BlockSizeX>\n",
    "#     <BlockSizeY>512</BlockSizeY>\n",
    "#     <BandsCount>3</BandsCount>\n",
    "#     <MaxConnections>10</MaxConnections>\n",
    "# </GDAL_WMS>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:notebook] *",
   "language": "python",
   "name": "conda-env-notebook-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
